﻿using System;
using System.Collections.Generic;
using System.Text;

using NORM.Common;
using NORM.Entity; 

namespace NORM.SQLObject
{
    using NORM.DataBase;

    public class EntityQuery<T> : List<T> where T : EntityBase, new()
    {
        private static EntityQuery<T> _instance = null;
        private static object lockobject = new object();

        public static EntityQuery<T> Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (lockobject)
                    {
                        if (_instance == null)
                        {
                            _instance = new EntityQuery<T>();                           
                        }
                    }
                }
                _instance._dba.sqlstatementclear();
                return _instance;
            }
        }

        private DataBase _dba;

        private string _nam;
        public string EntityName
        {
            get
            {
                return _nam;
            }
        }

        private EntityQuery()
        {           
            _dba = DataBaseFactory.Create();
            _nam = ((EntityBase)Activator.CreateInstance(typeof(T))).TableName;
        }

        public bool Insert(T model, DataBase db)
        {             
            return db.Insert<T>(model);
        }

        public bool Update(T model, DataBase db)
        {            
            return db.Update<T>(model);
        }

        public bool Delete(T model, DataBase db)
        {            
            return db.Delete<T>(model);
        }

        public bool Delete(object PrimaryKeyValue, DataBase db)
        {            
            return db.Delete<T>(PrimaryKeyValue);
        }

        public T QueryObject(OQL oql)
        {
            return _dba.QueryObject<T>(oql);
        }

        public T QueryObject(OQL oql, DataBase db)
        {
            return db.QueryObject<T>(oql);
        }

        public T QueryModel(object PrimaryKeyValue)
        {
            return _dba.QueryModel<T>(PrimaryKeyValue);
        }

        public T QueryModel(object PrimaryKeyValue, DataBase db)
        {
            return db.QueryModel<T>(PrimaryKeyValue);
        }

        public List<T> QueryList(OQL oql)
        {
            return _dba.QueryList<T>(oql);
        }

        public List<T> QueryList(OQL oql, DataBase db)
        {
            return db.QueryList<T>(oql);
        }

        public List<T> QueryList(OQL oql, PageLimit page)
        {
            return _dba.QueryList<T>(oql, page);
        }

        public List<T> QueryList(OQL oql, PageLimit page, DataBase db)
        {
            return db.QueryList<T>(oql, page);
        }

        public static List<T> QueryToList(OQL oql, PageLimit page, DataBase db)
        {
            return Instance.QueryList(oql, page, db);
        }

        public static List<T> QueryToList(OQL oql, DataBase db)
        {
            return Instance.QueryList(oql, db);
        }

        public static T QueryToModel(object PrimaryKeyValue, DataBase db)
        {
            return Instance.QueryModel(PrimaryKeyValue, db);
        }

        public static T QueryToObject(OQL oql, DataBase db)
        {
            return Instance.QueryObject(oql, db);
        }
 
    }
}
